package com.lky.controller;

import com.lky.domain.Planeinformation;
import com.lky.domain.Planetype;
import com.lky.service.PlaneinformationService;
import com.lky.service.PlanetypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 名称:PlanetypeController
 * 描述:PlanetypeController的控制类
 *
 * @version 1.0
 * @author:likaiyang
 * @datetime:2023-11-22 20:24
 */
@Controller
@RequestMapping("/planeinformation")
public class PlaneinformationController {

    @Autowired
    private PlaneinformationService planeinformationService;

    //查询全部飞机类型
    @RequestMapping("/findAllPlaneinformation")
    public String findAllPlaneinformation(Model model){
        List<Planeinformation> planeinformationList=planeinformationService.findAllPlaneinformation();
        model.addAttribute("allplaneinformation",planeinformationList);
        return "/planeinformation/allplaneinformation";
    }
    //查询全部飞机类型-普通用户
    @RequestMapping("/findAllPlaneinformation_new")
    public String findAllPlaneinformation_new(Model model){
        List<Planeinformation> planeinformationList=planeinformationService.findAllPlaneinformation();
        model.addAttribute("allplaneinformation",planeinformationList);
        return "/planeinformation/allplaneinformation_new";
    }

    //从系统首页跳转到添加页面
    @RequestMapping("/toinsertPlaneinformation")
    public String toinsertPlaneinformation(){
        return "/planeinformation/insertplaneinformation";
    }
    //添加用户，成功则再次执行查询全部用户
    @RequestMapping("/insertPlaneinformation")
    public String insertPlaneinformation(Planeinformation planeinformation, Model model){
        try{
            int i=planeinformationService.insertPlaneinformation(planeinformation);
            if(i>0){
                List<Planeinformation> planeinformationList=planeinformationService.findAllPlaneinformation();
                model.addAttribute("allplaneinformation",planeinformationList);
                model.addAttribute("msg","添加飞机成功");
                return "/planeinformation/allplaneinformation";
            }
            else{
                List<Planeinformation> planeinformationList=planeinformationService.findAllPlaneinformation();
                model.addAttribute("allplaneinformation",planeinformationList);
                model.addAttribute("msg","添加飞机失败");
                return "/planeinformation/allplaneinformation";
            }
        }catch (Exception e){
            List<Planeinformation> planeinformationList=planeinformationService.findAllPlaneinformation();
            model.addAttribute("allplaneinformation",planeinformationList);
            model.addAttribute("msg","添加飞机失败，可能原因为飞机名称重复或外键关联问题");
            return "/planeinformation/allplaneinformation";
        }
    }

    //根据id查询，用于更新和删除
    @RequestMapping("/findPlaneinformationById")
    public Planeinformation findPlaneinformationById(@RequestParam(name = "id") Integer id){
        Planeinformation planeinformation=planeinformationService.findPlaneinformationById(id);
        return planeinformation;
    }
    //跳转到修改用户信息的页面
    @RequestMapping("toupdatePlaneinformationById/{id}")
    public String toupdatePlaneinformationById(@PathVariable(name = "id")Integer id, Model model){
        Planeinformation planeinformation=planeinformationService.findPlaneinformationById(id);
        model.addAttribute("planeinformation",planeinformation);
        return "/planeinformation/updateplaneinformation";
    }
    //根据id更新用户信息
    @RequestMapping("/updatePlaneinformation")
    public String updatePlaneinformation(Planeinformation planeinformation,Model model){
        try{
            int i=planeinformationService.updatePlaneinformationById(planeinformation);
            if(i>0){
                List<Planeinformation> planeinformationList=planeinformationService.findAllPlaneinformation();
                model.addAttribute("allplaneinformation",planeinformationList);
                model.addAttribute("msg","修改飞机信息成功");
                return "/planeinformation/allplaneinformation";
            }
            else{
                List<Planeinformation> planeinformationList=planeinformationService.findAllPlaneinformation();
                model.addAttribute("allplaneinformation",planeinformationList);
                model.addAttribute("msg","修改失败");
                return "/planeinformation/allplaneinformation";
            }
        }catch (Exception e){
            List<Planeinformation> planeinformationList=planeinformationService.findAllPlaneinformation();
            model.addAttribute("allplaneinformation",planeinformationList);
            model.addAttribute("msg","修改飞机信息失败，可能原因为飞机名称重复或外键关联问题");
            return "/planeinformation/allplaneinformation";
        }
    }

    //根据id删除
    @RequestMapping("/deletePlaneinformationById/{id}")
    public String deletePlaneinformationById(@PathVariable(name = "id")Integer id,Model model){
        int i=planeinformationService.deletePlaneinformationById(id);
        if(i>0){
            List<Planeinformation> planeinformationList=planeinformationService.findAllPlaneinformation();
            model.addAttribute("allplaneinformation",planeinformationList);
            model.addAttribute("msg","删除飞机信息成功");
            return "/planeinformation/allplaneinformation";
        }
        else{
            List<Planeinformation> planeinformationList=planeinformationService.findAllPlaneinformation();
            model.addAttribute("allplaneinformation",planeinformationList);
            model.addAttribute("msg","删除失败");
            return "/planeinformation/allplaneinformation";
        }
    }

    //模糊查询
    @RequestMapping("/findPlaneinformationByStr")
    public String findPlaneinformationByStr(@RequestParam(value="searchStr") String searchStr,Model model){
        List<Planeinformation> planeinformationList=planeinformationService.findPlaneinformationByStr(searchStr);
        model.addAttribute("allplaneinformation",planeinformationList);
        return "/planeinformation/allplaneinformation";
    }
    //模糊查询-普通用户
    @RequestMapping("/findPlaneinformationByStr_new")
    public String findPlaneinformationByStr_new(@RequestParam(value="searchStr") String searchStr,Model model){
        List<Planeinformation> planeinformationList=planeinformationService.findPlaneinformationByStr(searchStr);
        model.addAttribute("allplaneinformation",planeinformationList);
        return "/planeinformation/allplaneinformation_new";
    }

    //跳转到飞机详情展示
    @RequestMapping("toPlaneById/{id}")
    public String toPlaneById(@PathVariable(name = "id")Integer id, Model model){
        Planeinformation planeinformation=planeinformationService.findPlaneinformationById(id);
        model.addAttribute("planeinformation",planeinformation);
        return "/planeinformation/plane";
    }

    @RequestMapping("/findPlaneinformationByTypeid/{id}")
    public String findPlaneinformationByTypeid(@PathVariable(name = "id")Integer id,Model model){
        List<Planeinformation> planeinformationList=planeinformationService.findPlaneinformationByTypeid(id);
        model.addAttribute("allplaneinformation",planeinformationList);
        return "/planeinformation/allplaneinformation";
    }

    //普通用户
    @RequestMapping("/findPlaneinformationByTypeid_new/{id}")
    public String findPlaneinformationByTypeid_new(@PathVariable(name = "id")Integer id,Model model){
        List<Planeinformation> planeinformationList=planeinformationService.findPlaneinformationByTypeid(id);
        model.addAttribute("allplaneinformation",planeinformationList);
        return "/planeinformation/allplaneinformation_new";
    }

    //跳转到飞机详情展示-普通用户
    @RequestMapping("toPlaneById_new/{id}")
    public String toPlaneById_new(@PathVariable(name = "id")Integer id, Model model){
        Planeinformation planeinformation=planeinformationService.findPlaneinformationById(id);
        model.addAttribute("planeinformation",planeinformation);
        return "/planeinformation/plane_new";
    }
}
